<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script type="importmap">
    {
        "imports":{
            "three":"../node_modules/three/build/three.module.js",
            "three/addons/": "../node_modules/three/examples/jsm/" 
        }
        
    }
</script>
<script type="module">
    import * as THREE from 'three';
    // import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
    import {OrbitControls} from '../node_modules/three/examples/jsm/controls/OrbitControls.js'
    
    const width=window.innerWidth;
    const height=window.innerHeight;

    // 创建场景
    const scene=new THREE.Scene();
    // 创建透视相机
    const camera=new THREE.PerspectiveCamera(45,width/height,0.1,1000);
    // 设置相机位置
    camera.position.set(200,200,200);
    // 设置相机观察点
    camera.lookAt(0,0,0);
    // 创建几何体
    const geometry=new THREE.BoxGeometry(200,200,200);
    // 创建基础材质
    // const material = new THREE.MeshBasicMaterial( { color: 0x0000ff,transparent:true,opacity:0.6 } );
    // 创建漫反射材质
    const material = new THREE.MeshLambertMaterial( { color: 0x0000ff,transparent:true,opacity:0.6 } );
    // 基于几何体和材质创建立体网格模型
    const mesh=new THREE.Mesh(geometry,material);
    // 设置网格模型的位置
    mesh.position.set(0,0,0);
    // 将立体网格模型添加到场景中
    scene.add(mesh);


    // 创建点光源
    const pointLight=new THREE.PointLight(0xffffff,1);
    // 设置点光源位置
    pointLight.position.set(200,100,160);
    // 将光源添加到场景中
    scene.add(pointLight);

    // 创建环境光
    const ambientLight=new THREE.AmbientLight(0xffffff,0.8);
    // 将自然光添加到场景值
    scene.add(ambientLight);

    // 创建平行光
    const directionalLight=new THREE.DirectionalLight(0xffffff,1);
    // 设置平行光的位置
    directionalLight.position.set(100,100,100);
    // 设置平行光的照射目标
    directionalLight.target=mesh;
    // 将平行光添加到场景中
    scene.add(directionalLight);


    // 创建三维坐标轴
    const axesHelper=new THREE.AxesHelper(300);
    // 将三维坐标轴添加到场景中
    scene.add(axesHelper);


    // 创建渲染器
    const renderer=new THREE.WebGLRenderer();
    // 设置渲染尺寸
    renderer.setSize(width,height);
    // 执行一个渲染操作
    renderer.render(scene,camera);

    // 创建相机轨道控制器
    const controls=new OrbitControls(camera,renderer.domElement);
    controls.addEventListener('change',()=>{
        // 重新执行渲染操作
        renderer.render(scene,camera);
    });

    // 将渲染结果挂载到dom树上
    document.body.appendChild(renderer.domElement)
</script>

</html>